﻿function ShowPopup(Url, FunctionCallBack, RedirectURL, dxWidth, dxHeight, btncloseid, paraArray, title) {
    var div = "<div id='divDialog'></div>";
    $(div).load(GetVirtualPath(Url), function (request, reponse, status, xhr) {
        if (request.indexOf("frmLogin") != -1) {
            window.location.href = GetVirtualPath("/Account/Login");
            return;
        }
        if (status == "error") {
            $(this).html(reponse);
        }
        var _width = dxWidth == null ? 'auto' : dxWidth;
        var _height = dxHeight == null ? 'auto' : dxHeight;
        var _divContent = $('#admin-main-content');
        var _div;


        if (_divContent.length > 0) {
            _div = _divContent;
        }
        else {
            _divContent = $('#wrapper');
            if (_divContent.length > 0)
                _div = _divContent;
            else
                _div = window;
        }
        $(this).dialog({
            modal: true,
            height: _height,
            position: {
                my: 'center',
                at: 'center',
                of: _div
            },
            width: _width,
            autoOpen: false,
            resizable: false,
            close: function () {
                $('.ui-dialog').remove();
                $('.container').remove();
                $('.ui-widget-overlay').remove();
                $(this).dialog('destroy').remove();
            },
            draggable: true,
            closeOnEscape: true,
            open: function (event, ui) {
                if (FunctionCallBack && (typeof FunctionCallBack == "function")) {
                    FunctionCallBack(paraArray);
                }
                ClosePopup(btncloseid, RedirectURL);
            }
        });

        $(this).dialog('open');

        if (title != null)
            $('.ui-dialog .ui-dialog-titlebar:last').append(title);
        $('.ui-dialog .ui-dialog-titlebar-close span').live("click", function () {
            $('#divDialog').html('');
            $('#divDialog').remove();
            $('#divDialog').dialog('destroy').remove();
        });
    });
};

function ClosePopup(btncloseid, redirectURL) {
    //btncloseId id cua button close popup
    if (btncloseid != null) {
        $('#' + btncloseid).live('click', function (e) {
            $('#divDialog').html('');
            $('#divDialog').remove();
            $('#divDialog').dialog('destroy').remove();
            if (redirectURL != null) {
                window.location = GetVirtualPath(redirectURL);
            }

        })

    }
}


function getPositionForDialog(dialog) {

    var main = $("#admin-main-content");

    var _mainTop = $(main).offset().top;
    var _mainHeight = $(main).height();

    var dialogHeight = $(dialog).height();
    var dialogWidth = $(dialog).width();

    var posT = ((_mainHeight - dialogHeight) / 2) + _mainTop + 'px';
    var posL = ($(window).width() - dialogWidth) / 2 + 'px';
    //$(dialog).position([0, 0]);
    dialog.option("position", dialog.options.position);
}
